/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bussiness;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author cuong_000
 */
public class ValidateInput {

    private static final String EMAIL_PATTERN
            = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    private static final String FNAME_PATTERN = ".{3,15}";
    private static final String LNAME_PATTERN = ".{3,30}";

    public static ArrayList<String> validate(String fname, String lname, String email) {
        Pattern emailPattern = Pattern.compile(EMAIL_PATTERN);
        Pattern FnamePattern = Pattern.compile(FNAME_PATTERN);
        Pattern LnamePattern = Pattern.compile(LNAME_PATTERN);
        ArrayList<String> listErrors = new ArrayList<String>();
        Matcher matcherEmail = emailPattern.matcher(email);
        Matcher matcherFname = FnamePattern.matcher(fname);
        Matcher matcherLname = LnamePattern.matcher(lname);

        if (fname.isEmpty()) {
            listErrors.add("<b>Fail! </b> First name empty");
        } else {
            if (!matcherFname.matches()) {
                listErrors.add("<b>Fail! </b> Fisrt name incorrect format, between 3 - 15 characters");
            }
            if (fname.contains("  ")) {
                listErrors.add("<b>Fail! </b> Fisrt name incorrect format, have mutil space");
            }
        }
        if (lname.isEmpty()) {
            listErrors.add("<b>Fail! </b> Last name empty");
        } else {
            if (!matcherLname.matches()) {
                listErrors.add("<b>Fail! </b> Last name incorrect format, between 3 - 30 characters");
            }
            if (lname.contains("  ")) {
                listErrors.add("<b>Fail! </b> Last name incorrect format, have mutil space");
            }
        }
        if (email.isEmpty()) {
            listErrors.add("<b>Fail! </b> Email empty");
        } else {
            if (!matcherEmail.matches()) {
                listErrors.add("<b>Fail! </b> Email incorrect format , Example : example@mail.com");
            }
        }

        return listErrors;
    }
}
